package com.deram.ctin.serv.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.consts.ErrorConsts;
import com.deram.ctin.common.error.BusinessException;
import com.deram.ctin.common.utils.ArrayHandleUtils;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.business.entity.BaseUpdateInfo;
import com.deram.ctin.serv.business.entity.BusinessInfo;
import com.deram.ctin.serv.business.entity.BusinessShareholder;
import com.deram.ctin.serv.business.filter.BusinessShareholderFilter;
import com.deram.ctin.serv.business.mapper.BusinessShareholderMapper;
import com.deram.ctin.serv.business.service.BusinessInfoService;
import com.deram.ctin.serv.business.service.BusinessShareholderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jtt
 * @version 1.0, 2017/9/28
 */
@Service
public class BusinessShareholderServiceImpl implements BusinessShareholderService {

    @Autowired
    BusinessShareholderMapper businessShareholderMapper;

    @Autowired
    BusinessInfoService businessInfoService;

    @Override
    public BusinessShareholder find(Long id) {
        ValidateUtils.isId(id, "股东出资ID");
        BusinessShareholder result = businessShareholderMapper.selectById(id);
        BusinessUtils.notNull(result, "股东出资信息不存在");
        return result;
    }

    @Override
    public List<BusinessShareholder> list(BusinessShareholderFilter filter) {
        ValidateUtils.isId(filter.getEnterId(), "企业ID");
        EntityWrapper<BusinessShareholder> ew = createEntityWrapper(filter);
        List<BusinessShareholder> result = businessShareholderMapper.selectList(ew);
        return result;
    }

    @Override
    public List<BusinessShareholder> findByEnterId(Long id) {
        BusinessShareholder businessShareholder = new BusinessShareholder();
        businessShareholder.setEnterId(id);
        List<BusinessShareholder> result = businessShareholderMapper.findByEnterInfo(businessShareholder);
        return result;
    }

    @Override
    public BusinessShareholder save(BusinessShareholder param) {
        ValidateUtils.isId(param.getEnterId(), "企业ID");
        if (null != param.getId()) {
            BusinessShareholder finder = find(param.getId());
            businessShareholderMapper.updateById(param);
        } else {
            businessShareholderMapper.insert(param);
        }
        return param;
    }

    @Override
    public void delete(Long id) {
        businessShareholderMapper.deleteById(find(id));
    }

    @Override
    public void deleteByEnterId(Long id) {
        BusinessShareholderFilter filter = new BusinessShareholderFilter();
        filter.setEnterId(id);
        List<BusinessShareholder> lists = list(filter);
        if (null != lists && !lists.isEmpty()) {
            List<Long> ids = ArrayHandleUtils.readAttrList(lists, "id");
            businessShareholderMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public void saveList(BaseUpdateInfo<BusinessShareholder> updateInfo) {
        Long enterId = updateInfo.getEnterId();
        BusinessInfo business = businessInfoService.find(enterId);
        if (null == business) {
            throw new BusinessException(ErrorConsts.VALIDATE, "工商信息未找到");
        }
        deleteByEnterId(enterId);
        List<BusinessShareholder> lists = updateInfo.getList();
        for (BusinessShareholder item : lists) {
            item.setId(null);
            item.setEnterId(enterId);
        }
        businessShareholderMapper.batchInsert(lists);
    }

    @Override
    public PageResult<BusinessShareholder> findByEnterInfoPage(BusinessShareholderFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo,pageSize);
        
        EntityWrapper<BusinessShareholder> entityWrapper = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(businessShareholderMapper.selectPage(pagination,entityWrapper),pagination);
    }

    private EntityWrapper<BusinessShareholder> createEntityWrapper(BusinessShareholderFilter filter) {
        BusinessShareholder entity = new BusinessShareholder();
        entity.setEnterId(filter.getEnterId());
        entity.setYear(filter.getYear());
        EntityWrapper<BusinessShareholder> ew = MybatisUtils.createEntityWrapper(entity, filter);
        if(null != filter.getHasYear()){
            if(filter.getHasYear()){
                ew.isNotNull("year");
            } else {
                ew.isNull("year");
            }
        }
        return ew;
    }
}
